package com.naiterui.ehp.bs.cms.modules.base.service.impl;

import java.util.Date;
import java.util.Stack;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.naiterui.ehp.bp.security.utils.BaseJwtUser;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bs.cms.common.constants.DsName;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.base.mapper.CmsCityBaseMapper;
import com.naiterui.ehp.bs.cms.modules.base.service.ICmsCityService;
import com.naiterui.ehp.bs.cms.modules.base.vo.CityPageVO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 潘xin
 * @since 2021/9/13
 */
@Slf4j
@Service
@DS(DsName.EHP)
public class CmsCityServiceImpl implements ICmsCityService {
    @Autowired
    private CmsCityBaseMapper cmsCityMapper;

    @Override
    public PageVO<CityPageVO> queryPage(PageParamsVO pageParamsVO, CityPageVO cityPageVO) {
        QueryWrapper<CityPageVO> entityWrapper = this.buildEntityWrapper(pageParamsVO, cityPageVO);
        log.info("查询参数cityPageVO={}", cityPageVO.toString());
        Page<CityPageVO> page = cmsCityMapper.getPage(pageParamsVO.getPage(), entityWrapper);

        return PageVO.toPageVo(page);
    }

    @Override
    public CityPageVO queryCityById(Long cityId) {
        return cmsCityMapper.getById(cityId);
    }

    @Override
    public void saveOrUpdate(CityPageVO cityPageVO) {
        // 获取登录用户
        BaseJwtUser currentUser = SecurityUtils.getCurrentUser();
        String username = currentUser.getUsername();
        if (null == cityPageVO.getId()) {
            cityPageVO.setCreatedBy(username);
            cityPageVO.setCreatedAt(new Date());
        } else {
            cityPageVO.setChangedBy(username);
            cityPageVO.setChangedAt(new Date());
        }
        // 处理parentName父类全称
        StringBuffer parentName = new StringBuffer("");
        // fullName存放栈逆序获取
        Stack<String> stackName = new Stack<>();
        // 递归
        this.getCityFullNameById(Long.valueOf(cityPageVO.getParentId()), stackName);
        while (!stackName.isEmpty()) {
            parentName.append(stackName.pop());
        }
        cityPageVO.setParentName(parentName.toString());
        cityPageVO.setFullName(cityPageVO.getName());// 省市区全称
        cmsCityMapper.saveOrUpdateCity(cityPageVO);
    }

    // 递归获取父节点fullName
    private void getCityFullNameById(Long parentId, Stack<String> stackName) {
        CityPageVO city = cmsCityMapper.getById(parentId);
        if (stackName.isEmpty())
            stackName.push(city.getName());
        if (!stackName.isEmpty())
            stackName.push(city.getName() + ",");
        if (city.getParentId() != 0) {
            getCityFullNameById(Long.valueOf(city.getParentId()), stackName);
        }
    }

    @Override
    public void updateCityStatus(Long cityId, Integer status) {
        cmsCityMapper.updateCityStatus(cityId, status);
    }

    private QueryWrapper<CityPageVO> buildEntityWrapper(PageParamsVO pageParamsVO, CityPageVO cityPageVO) {
        QueryWrapper<CityPageVO> entityWrapper = pageParamsVO.getEntityWrapper("t");
        Integer id = cityPageVO.getId();
        entityWrapper.eq(id != null, "t.id", id);
        Integer type = cityPageVO.getType();
        entityWrapper.eq(type != null, "t.type", type);
        Integer parentId = cityPageVO.getParentId();
        entityWrapper.eq(parentId != null && parentId != 0, "t.parent_id", parentId);
        Boolean available = cityPageVO.getAvailable();
        entityWrapper.eq(available != null && available == true, "t.available", 1);
        entityWrapper.eq(available != null && available == false, "t.available", 0);
        String name = cityPageVO.getName();
        entityWrapper.like(StringUtils.isNotBlank(name), "t.name", name);
        return entityWrapper;
    }

}
